﻿/******************************************************   
创建者: 梁家健
创建时间: 2017-03-16
修改:
修改时间: 
功能: 店铺更新系数导入导出
 ******************************************************/

using com.nm.library.Excels;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using PlanAPI.model;
using com.nm.library.Helper;
using com.nm;
using com.nm.Helper;

namespace PlanAPI.Export
{
    [Export(typeof(ExcelImport))]
    public class PLBasShopUpdateRate : ExcelImport
    {
        private static int num = 0;

        public override ExcelImportType Type => ExcelImportType.PLBASSHOPUPDATERATE;
        public override Dictionary<string, ImportVerify> DictFields => _dictFields;

        private Dictionary<string, ImportVerify> _dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="公司代码",FieldName="CompanyID",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="年份",FieldName="Yearv",DataType=typeof(long),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="周",FieldName="Weekv",DataType=typeof(long),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="所属月份",FieldName="Monthv" ,DataType=typeof(long),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="商品区域",FieldName="ParentAreaName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="店铺ID",FieldName="ShopID",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="店铺代码",FieldName="ShopCode",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,false,true)},
            new ImportVerify{ ColumnName="店铺名称",FieldName="ShopName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,false,true)},
            new ImportVerify{ ColumnName="更新率系数（%）",FieldName="ShopRate",DataType=typeof(decimal),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,50,false,true)},
            new ImportVerify{ ColumnName="备注",FieldName="Remark" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,200,false,true)},
           }.ToDictionary(e => e.ColumnName, e => e);

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            var index = 0;
            var handler = new PLBasShopUpdateRateHandler();
            var uid = Guid.NewGuid().ToString().Replace("-", "");
            var tempTableName = $" goods.goods_{uid} ";
            var result = new ImportResult
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
            };
            try
            {
                var cesd = JsonHelper.CompanyIDExtrSourceDic().ToDictionary(d => d.CompanyID, d => d.ExtrSource);
                handler.DropTempTable(tempTableName);
                handler.CreateTempTable(dt, tempTableName, cesd);
                var errorDT = handler.ImportPLBasShopUpdateRate(tempTableName, cesd);
                result.Message =
                    $"成功导入{(dt.Rows.Count - errorDT.Rows.Count)}条数据{(errorDT.Rows.Count == 0 ? "" : $@",有{errorDT.Rows.Count}条错误数据")}";
                if (errorDT.Rows.Count != 0)
                {
                    var path = ExcelImporMapper.GetTemplatePath(Type);
                    result.Url = ExcelUtil.ExportErrorExeclToDB(errorDT, path, _dictFields, "店铺更新系数错误数据", num);
                    num++;
                }
            }
            catch (Exception ex)
            {
                Log.Error("导入店铺更新系数：" + ex);
                result.IsSuccess = false;
                result.ExtraInfo = ex;
                result.Message = ex.Message;
            }
            finally
            {
                handler.DropTempTable(tempTableName);
            }
            return result;
        }

        public override DataTable GetExportData(string Keys)
        {
            var handler = new PLBasShopUpdateRateHandler();
            return handler.ExportPLBasShopUpdateRate(Keys);
        }
    }
}
